package img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * @author wzby
 * @Date: 2022/8/3 11:22
 */
public class Img2 {
    public static void main(String[] args) {
        //彩色图像转换黑白图像（灰度）
        try {
            BufferedImage src = ImageIO.read(new File("ch09/src/pho/img2.png"));
            int w = src.getWidth();
            int h = src.getHeight();
            BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_BYTE_GRAY);
            Graphics2D g = img.createGraphics();
            g.drawImage(src,0,0,null);
            Random rand = new Random();
            for (int i = 0;i < 20;i++){
                g.setColor(new Color(255,255,0,rand.nextInt(50,100)));
                int size = rand.nextInt(10,90);
                int x = rand.nextInt(w);
                int y = rand.nextInt(h);
                g.fillOval(x,y,size,size);
                //.filloval圆形图像
            }
            g.dispose();
            ImageIO.write(img,"png",new File("ch09/src/pho/imga2.png"));
            Runtime.getRuntime().exec("cmd /k start ch09/src/pho/imga2.png");
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }
}
